package com.lupus.business.inspector;

import java.util.*;
import javax.servlet.http.*;
import com.lupus.business.Inspectable;
import com.lupus.common.VariableFactory;

public final class LoginInspector implements Inspectable {

	public boolean preInspect(Map<String,Object> requestMap, Map<String,Object> responseMap,HttpServletRequest request,HttpServletResponse response){
		Object frontCoder = requestMap.get("validateCoder");
		Object backCoder = request.getSession().getAttribute("validateCoder");
		boolean inspectResult = backCoder!=null&&backCoder.equals(frontCoder);
		if(!inspectResult){
			responseMap.put("returnFlag",VariableFactory.FAIL);
			responseMap.put("returnTip","common.tip.validatecoderwrong");
		}
		return inspectResult;
	}

	public boolean sufInspect(Map<String,Object> requestMap, Map<String,Object> responseMap,HttpServletRequest request,HttpServletResponse response){
		boolean inspectResult = false;
		Object password = requestMap.get("password");
		List<Map> result = (List)responseMap.get("dataValues");
		for(int i=0;i<result.size();i++){
			Map<String,Object> recordMap = (Map)result.get(i);
			if(password!=null&&password.equals(recordMap.get("PASSWORD"))){
				request.getSession().setAttribute("currentUser",recordMap);
				inspectResult = true;
				break;
			}
		}
		if(!inspectResult){
			responseMap.put("returnFlag",VariableFactory.FAIL);
			responseMap.put("returnTip","common.tip.usernamepasswordwrong");
		}
		return inspectResult;
	}
}
